#include <stdio.h>
#include <unistd.h> 
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>

#include <network.h>
#include <server.h>

void disconnect(struct client *);
void cleanClient(struct client *);
char *createmessage(char *, char *);

void *lobby(void *clientura){
	struct client *client = clientura;
	printf("Клиент ID:%d зашёл в лобби.\n", client->id);
	char *receiveMessage;
	char *sendMessage;
	int code;
	if(reserve > 0) reserveClient(client->id, client->name, client->flag);
	while((client->sockfd > 0) && ((recv(client->sockfd, &code, sizeof(int), MSG_PEEK)) > 0)){
		receiveMessage = receiving(client->sockfd);	
		switch(code){
			case CHANGENAME:{
				if (strlen(receiveMessage) > 12) receiveMessage[11] = '\0'; 
				printf("Пользователь ID:%d, %s изменил ник на %s.\n", client->id, client->name, receiveMessage);
				strcpy (client->name, receiveMessage);
				sendUserList();
				if(reserve > 0) reserveName(client->id, client->name);
				break;
			}
			case BROADCAST:{
				sendMessage = createmessage(client->name, receiveMessage);
				int i;
				for (i = 0; i < MAXCONNECTION; i++){
					if (clients[i].sockfd < 1) continue;
					sending(clients[i].sockfd, MESSAGE, sendMessage);
				}
				break;
			}
			case ALTSERVER:{
				printf("Аккумулятор Заряжен\n");
				strcpy (client->name, "ALTSERVER");
				
				reserve = client->sockfd;
				sendBackup (client->sockfd);
				break;
			}
			case REQID:{
				char bufferID[4];
				sprintf(bufferID, "%d", client->id);
				sending(client->sockfd, YOURID, bufferID);
				break;
			}
			
			case ASWID:{
				int i;
				int id = atoi(receiveMessage);
				printf("RECONNECT ID %d",id);
				for(i = 0; i < MAXCONNECTION; i++){
					if(clients[i].id != id) continue;
					strcpy(client->name, clients[i].name);
					client->flag = clients[i].flag;
					cleanClient(&clients[i]);
					printAllInfo();
					
					char bufferID[4];
					sprintf(bufferID, "%d", client->id);
					sending(client->sockfd, YOURID, bufferID);
					break;
				}
				break;
			}
			default:{
				sending(client->sockfd, WRONGCODE, NULL);
				printf("Паренек ID:%d был выброшен из сервера.\n", client->id);
				shutdown(client->sockfd, 2);
				close(client->sockfd);
				return;
			}
		}
	}
	printf("Клиент ID:%d, %s отключился от сервера.\n", client->id, client->name);
	disconnect(client);
	return;
}

void sendUserList(){
	int i,  j;
	for (i = 0; i < MAXCONNECTION; i++){
		if (clients[i].sockfd < 1) continue;
		sending(clients[i].sockfd, STARTUSERLIST, NULL);
		for (j = 0; j < MAXCONNECTION; j++){
			if (clients[j].sockfd < 1) continue;
			sending(clients[i].sockfd, USERLIST, clients[j].name);
		}
	}
}

char *createmessage(char *string1, char *string2){
	int size1, size2;
	size1 = strlen(string1);
	size2 = strlen(string2);
	
	char *newstring = calloc(size1 + size2, sizeof(char) + 1); // Массив под сообщение.
	strcat(newstring, string1);
	strcat(newstring, ":");
	strcat(newstring, string2);
	
	return newstring;
}

void disconnect(struct client *client){
	if(client->sockfd != reserve){
		if(reserve > 0) reserveClientQuit(client->id);
	}
	if(client->sockfd == reserve){
		reserve = 0; 
	}
	close(client->sockfd);
	client->id = -1;
	client->sockfd = -1;
	strcpy (client->name, "Anonymouse");
	sendUserList();
}

void cleanClient(struct client *client){
	client->id = -1;
	client->sockfd = -1;
	strcpy (client->name, "Anonymouse");
}
